const {Controller} = require('egg');

class UserController extends Controller{
    async registry(){
        let {ctx,service} = this;
        //post ctx.request.body  get ctx.query
        let {username,password} = ctx.request.body;
        console.log(username,password);
        if(username && password){
            //1.排重
            let target = await service.user.selectUser(username);
            if(target.length){
                ctx.body = {
                    code:3,
                    msg:'此人已存在'
                }
            }else{
                try{
                    let hmcPwd = ctx.helper.hmc(password);
                    await service.user.registry(username,hmcPwd);
                    ctx.body = {
                        code:1,
                        msg:'注册成功'
                    }
                }catch(e){
                    ctx.body = {
                        code:0,
                        msg:e
                    }
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
        
    }
}

module.exports = UserController